using System;
using System.Data;
using System.Data.SqlClient;
using MirLda.BL.DB.Fields;
using MirLda.BL.Objects;
using MirLda.BL.Objects.News;

namespace MirLda.BL.DB.Facades
{
	/// <summary>
	/// NewsItemFacade.
	/// </summary>
	internal class NewsItemFacade
	{
		#region constructor
		private NewsItemFacade()
		{}
		#endregion

		#region get newsItems

		internal static NewsItemCollection GetNews( Guid currentUserId, PageListingObj pager )
		{
			NewsItemCollection result = new NewsItemCollection();

			SqlCommand cmd = new SqlCommand("NewsItemsGet", new SqlConnection(Implements.Implementations.GetConfigImpl().ConnectionString));
			cmd.CommandType = CommandType.StoredProcedure;
			if( currentUserId != Guid.Empty )
				cmd.Parameters.Add( "@CurrentUserId", currentUserId );
			if( pager != null )
			{
				cmd.Parameters.Add( "@PageNumber", pager.CurrentPage );
				cmd.Parameters.Add( "@RecordsOnpage", pager.RecordsOnPage );
			}
			
			cmd.Connection.Open();

			using(IDataReader dr = cmd.ExecuteReader())
			{
				while( dr.Read() )
				{
					int newsItemId = NewsItemDBFields.GetNewsItemId(dr);
					NewsItem item = new NewsItem(newsItemId);
					item.NewsItemTitle = NewsItemDBFields.GetNewsItemTitle(dr);
					item.NewsItemAnnonce = NewsItemDBFields.GetNewsItemAnnonce(dr);
					item.NewsItemContent = NewsItemDBFields.GetNewsItemContent(dr);

					result.Add( item );
				}

				dr.Close();
			}
			cmd.Connection.Close();

			return result;
		}

		#endregion
	}
}
